home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / util / flyFonts / FontsDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-23  |  5.2 KB  |  255 lines

  1. // FontsDlg.cpp : implementation file
  2. //
  3.  
  4. #include <afxwin.h>         // MFC core and standard components
  5. #include <afxext.h>         // MFC extensions
  6. #include <afxdtctl.h>        // MFC support for Internet Explorer 4 Common Controls
  7. #ifndef _AFX_NO_AFXCMN_SUPPORT
  8. #include <afxcmn.h>            // MFC support for Windows Common Controls
  9. #endif // _AFX_NO_AFXCMN_SUPPORT
  10.  
  11. #include "Fonts.h"
  12. #include "FontsDlg.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. int size=128,flag=0;
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CFontsDlg dialog
  24.  
  25. CFontsDlg::CFontsDlg(CWnd* pParent /*=NULL*/)
  26.     : CDialog(CFontsDlg::IDD, pParent)
  27. {
  28.     //{{AFX_DATA_INIT(CFontsDlg)
  29.         // NOTE: the ClassWizard will add member initialization here
  30.     //}}AFX_DATA_INIT
  31.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  32. }
  33.  
  34. void CFontsDlg::DoDataExchange(CDataExchange* pDX)
  35. {
  36.     CDialog::DoDataExchange(pDX);
  37.     //{{AFX_DATA_MAP(CFontsDlg)
  38.     DDX_Control(pDX, IDC_SAVE, m_save);
  39.     DDX_Control(pDX, IDC_256, m_256);
  40.     DDX_Control(pDX, IDC_128, m_128);
  41.     //}}AFX_DATA_MAP
  42. }
  43.  
  44. BEGIN_MESSAGE_MAP(CFontsDlg, CDialog)
  45.     //{{AFX_MSG_MAP(CFontsDlg)
  46.     ON_WM_PAINT()
  47.     ON_WM_QUERYDRAGICON()
  48.     ON_BN_CLICKED(IDC_FONT, OnFont)
  49.     ON_BN_CLICKED(IDC_SAVE, OnSave)
  50.     ON_BN_CLICKED(IDC_256, On256)
  51.     ON_BN_CLICKED(IDC_128, On128)
  52.     //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CFontsDlg message handlers
  57.  
  58. BOOL CFontsDlg::OnInitDialog()
  59. {
  60.     CDialog::OnInitDialog();
  61.  
  62.     SetIcon(m_hIcon, TRUE);            // Set big icon
  63.     SetIcon(m_hIcon, FALSE);        // Set small icon
  64.  
  65.     memset(&lf,0,sizeof(lf));
  66.     
  67.     if (size==128)
  68.         m_128.SetState(1);
  69.     else 
  70.     {
  71.         size=256;
  72.         m_128.SetState(2);
  73.     }
  74.  
  75.     m_save.EnableWindow(0);
  76.  
  77.     return TRUE;  // return TRUE  unless you set the focus to a control
  78. }
  79.  
  80. // If you add a minimize button to your dialog, you will need the code below
  81. //  to draw the icon.  For MFC applications using the document/view model,
  82. //  this is automatically done for you by the framework.
  83.  
  84. void CFontsDlg::OnPaint() 
  85. {
  86.     if (IsIconic())
  87.     {
  88.         CPaintDC mydc(this); // device context for painting
  89.  
  90.         SendMessage(WM_ICONERASEBKGND, (WPARAM) mydc.GetSafeHdc(), 0);
  91.  
  92.         // Center icon in client rectangle
  93.         int cxIcon = GetSystemMetrics(SM_CXICON);
  94.         int cyIcon = GetSystemMetrics(SM_CYICON);
  95.         CRect rect;
  96.         GetClientRect(&rect);
  97.         int x = (rect.Width() - cxIcon + 1) / 2;
  98.         int y = (rect.Height() - cyIcon + 1) / 2;
  99.  
  100.         // Draw the icon
  101.         mydc.DrawIcon(x, y, m_hIcon);
  102.     }
  103.     else
  104.     {
  105.         DrawBMP();
  106.     }
  107. }
  108.  
  109. HCURSOR CFontsDlg::OnQueryDragIcon()
  110. {
  111.     return (HCURSOR) m_hIcon;
  112. }
  113.  
  114. void CFontsDlg::OnFont() 
  115. {
  116.     CFontDialog    fd(&lf);
  117.  
  118.     if (fd.DoModal()==IDOK)
  119.     {
  120.         fd.GetCurrentFont( &lf );
  121.         SetDlgItemText( IDC_FONTNAME, fd.GetFaceName());
  122.         flag=1;
  123.         m_save.EnableWindow(1);
  124.         Invalidate(1);
  125.     }
  126. }
  127.  
  128. void CFontsDlg::DrawBMP()
  129. {
  130.     if (flag)
  131.     {
  132.     char str[2]=" ";
  133.     CFont f;
  134.     CSize len;
  135.     int n;
  136.     int x,y;
  137.     CRect rect;
  138.     GetClientRect(&rect);
  139.     x=10;
  140.     y=40;
  141.  
  142.     CPaintDC mydc(this);
  143.     mydc.FillSolidRect(x,y,size,size,0 );
  144.  
  145.     mydc.SetTextColor(0xffffffff);
  146.     f.CreateFontIndirect(&lf);
  147.     mydc.SelectObject(f);
  148.  
  149.     for (int i=0; i<8; i++)
  150.         for (int j=0; j<8; j++)
  151.         {
  152.             n=' '+i+j*8;
  153.             if (n>='A' && n<='Z')
  154.                 str[0]=n+(int)'a'-(int)'A';
  155.             else str[0]=n;
  156.             mydc.TextOut(x+2+i*size/8,y+j*size/8,str);
  157.         }
  158.     }
  159.     CDialog::OnPaint();
  160. }
  161.  
  162. void CFontsDlg::OnSave() 
  163. {
  164.     char str[2]=" ";
  165.     CFont f;
  166.     CSize len;
  167.     int n;
  168.  
  169. //    CFileDialog fd(0,"tga",0,OFN_OVERWRITEPROMPT,"TGA files|*.tga||",this);
  170. //    if (fd.DoModal()==IDOK)
  171.     {
  172.  
  173.     FILE *fp=fopen( "fonts.tga","wb");
  174. //    FILE *fp=fopen( fd.GetPathName(),"wb");
  175.     if (fp)
  176.     {
  177.         unsigned char TGA_INI[18];
  178.         CDC *mydc=GetDC();
  179.         POINT p;
  180.  
  181.         int x,y;
  182.         CRect rect;
  183.         GetClientRect(&rect);
  184. //        x=rect.right/2-size/2;
  185. //        y=(rect.bottom-size)*2/3;
  186.         x=20;
  187.         y=40;
  188.  
  189.         memset(&TGA_INI[0],0,18);
  190.         TGA_INI[12]=(unsigned char)(size%256);
  191.         TGA_INI[13]=(unsigned char)(size/256);
  192.         TGA_INI[14]=(unsigned char)(size%256);
  193.         TGA_INI[15]=(unsigned char)(size/256);
  194.         TGA_INI[2]=2;
  195.         TGA_INI[16]=8*4;
  196.         fwrite((char *)&TGA_INI[0],18,1,fp);
  197.  
  198.         for( p.y=size+y-1;p.y>=y;p.y-- )
  199.             for( p.x=x;p.x<size+x;p.x++ )
  200.             {
  201.                 COLORREF c=mydc->GetPixel(p);
  202.                 fwrite(&c,sizeof(c),1,fp);
  203.             }
  204.  
  205.         fclose(fp);
  206.         ReleaseDC(mydc);
  207.     }
  208.  
  209.     fp=fopen("fonts.txt","wt");
  210. /*    char str[256];
  211.     strcpy(str,fd.GetPathName());
  212.     if (strrchr(str,'.'))
  213.         *strrchr(str,'.')=0;
  214.     strcat(str,".txt");
  215.     fp=fopen(str,"wt");*/
  216.     if (fp)
  217.     {
  218.         CDC *mydc=GetDC();
  219.         mydc->SetTextColor(0xffffffff);
  220.         f.CreateFontIndirect(&lf);
  221.         mydc->SelectObject(f);
  222.  
  223.         int i,j;
  224.         for( j=0;j<8;j++ )
  225.             for( i=0;i<8;i++ )
  226.             {
  227.             n=' '+i+j*8;
  228.             if (n>='A' && n<='Z')
  229.                 str[0]=n+(int)'a'-(int)'A';
  230.             else str[0]=n;
  231.             len=mydc->GetTextExtent(str,1);
  232.             fprintf(fp,"%i\n",(len.cx+2)/2);
  233.             }
  234.         ReleaseDC(mydc);
  235.         fclose(fp);
  236.     }
  237.     }
  238. }
  239.  
  240. void CFontsDlg::On256() 
  241. {
  242.     size=256;
  243.     Invalidate(1);
  244.     m_128.SetState(0);
  245.     m_256.SetState(1);
  246. }
  247.  
  248. void CFontsDlg::On128() 
  249. {
  250.     size=128;
  251.     Invalidate(1);
  252.     m_128.SetState(1);
  253.     m_256.SetState(0);
  254. }
  255.